<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="./js/LinkList.js"></script>
    <script>
        // 散列表子类
        class ValuePair {
            constructor(key, value) {
                this.key = key;
                this.value = value;
            }

            toString() {
                return `[${this.key}--${this.value}]`
            }
        }

        // 散列表实现
        class HashTable {
            constructor() {
                this.table = [];
            }

            // 对载入数据的预处理
            static loseloseHashCode(key) {
                let hash = 0;
                for (let i = 0; i < key.length; i++) {
                    hash += key.charCodeAt(i);
                }
                return hash % 37;
            }

            // 向散列表中插入值
            put(key, value) {
                let position = HashTable.loseloseHashCode(key);
                if (this.table[position] === undefined) {
                    this.table[position] = new LinkedList();
                }
                this.table[position].append(new ValuePair(key, value));
            }

            // 获取散列表中key对应的数值
            get(key) {
                let position = HashTable.loseloseHashCode(key);
                if (this.table[position] !== undefined) {
                    let current = this.table[position].head;
                    // 注意：这里无法检查到最后一个元素
                    while (current.next) {
                        if (current.element.key === key) {
                            return current.element.value;
                        }
                        current = current.next;
                    }
                    // 检查最后一个链表元素
                    if (current.element.key === key) {
                        return current.element.value;
                    }
                } else {
                    return undefined;
                }
            }

            // remove移除元素
            remove(key) {
                let position = HashTable.loseloseHashCode(key);
                if (this.table[position] !== undefined) {
                    let current = this.table[position].head;
                    // 注意：这里无法检查到最后一个元素
                    while (current.next) {
                        if (current.element.key === key) {
                            this.table[position].remove(current.element);
                            if (this.table[position].isEmpty()) {
                                this.table[position] = undefined;
                            }
                            return true;
                        }
                        current = current.next;
                    }
                    // 检查最后一个链表元素
                    if (current.element.key === key) {
                        this.table[position].remove(current.element);
                        if (this.table[position].isEmpty()) {
                            this.table[position] = undefined;
                        }
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }


        let table = new HashTable();
        table.put('haha', 10);
        table.put('pa', 20);
        table.put('rua', 30);
        table.remove('rua');
        console.log(table);
    </script>
</body>

</html>